<template>
  <div class="tagListPage">
    <div class="formSearch">
      <div class="searchItem">
        <span>标签名称：</span>
        <el-input
          v-model="query.tagName"
          maxlength="20"
          class="label"
          placeholder="请输入内容"
        />
      </div>
      <div class="searchItem">
        <span>客户数量：</span>
        <el-input v-model="query.minPerson" maxlength="9" class="nums" />
        &nbsp;至&nbsp;
        <el-input v-model="query.maxPerson" maxlength="9" class="nums" />
      </div>
      <div class="btns">
        <el-button type="primary" @click="search">查询</el-button>
        <el-button type="primary" @click="clear">重置</el-button>
        <el-button type="primary" @click="addTag">添加标签</el-button>
        <el-button type="danger" @click="deleteTagFn">删除</el-button>
      </div>
    </div>
    <!-- 表格 -->
    <div class="tableBox">
      <el-table
        v-loading="tableLoading"
        :data="tableData"
        border
        :header-cell-style="{ background: '#EEF3FF', color: '#333333' }"
        tooltip-effect="dark"
        style="width: 100%"
        class="dataTable"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column
          prop="labelName"
          label="标签名称"
          show-overflow-tooltip
        />
        <el-table-column prop="remark" label="备注信息" show-overflow-tooltip />
        <el-table-column prop="person" label="客户数量" show-overflow-tooltip />
        <el-table-column label="操作" show-overflow-tooltip>
          <template #default="scope">
            <div class="btnList">
              <el-button
                type="primary"
                link
                @click="edit(scope.row)"
              >编辑</el-button>
              <el-popconfirm
                title="确定删除此标签？"
                @confirm="deleteTagFn(scope.row)"
              >
                <template #reference>
                  <el-button
                    class="delCls"
                    link
                    type="danger"
                  >删除</el-button>
                </template>
              </el-popconfirm>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="fenye">
        <el-pagination
          :current-page="page.page"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="page.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          background
          :total="page.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
    <!-- 新建弹框 -->
    <el-dialog
      v-model="visible.data"
      :close-on-click-modal="false"
      :title="form.labelId ? '修改标签' : '新增标签'"
      width="30%"
      top="50px"
      class="group-dialog"
    >
      <div class="addTagBox">
        <el-form
          ref="tagFormRef"
          :inline="true"
          :model="form"
          :rules="rules"
          label-width="90px"
        >
          <el-form-item label="标签名称" prop="labelName">
            <el-input
              v-model="form.labelName"
              placeholder="请输入内容"
              maxlength="10"
              onblur="value=value.replace(/(^\s*)|(\s*$)/g, '')"
              show-word-limit
            />
          </el-form-item>
          <el-form-item label="备注信息">
            <el-input
              v-model="form.remark"
              maxlength="200"
              type="textarea"
              :rows="5"
              placeholder="请输入内容"
            />
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <el-button type="primary" @click="saveTag">保 存</el-button>
        <el-button @click="closeAddTag">取 消</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { defineComponent, onMounted, reactive, ref } from 'vue';
import {
  addLabel,
  getLabels,
  labelEdit,
  labelUpdate,
  deleteLabel,
} from '@/api/customer'

defineComponent({
  name: 'TagList'
})

const tagFormRef = ref(null)
const tableLoading = ref(false)
let visible = reactive({
  data: false,
})

let page = reactive({
  page: 1,
  pageSize: 10,
  total: 0
})
let query = reactive({
  labelName: '',
  maxPerson: null,
  minPerson: null,
})

let tableData = ref([]);

// 初始化查询所有数据
function getTagAll () {
  tableLoading.value = true
  getLabels(Object.assign(
    {},
    page,
    query,
  )).then(res => {
    tableData.value = res.data.list
    page.total = res.data.total
  }).finally(() => {
    tableLoading.value = false
  })
}
function handleCurrentChange (val) {
  page.page = val
  getTagAll()
}

function handleSizeChange (val) {
  page.pageSize = val
  getTagAll()
}

//  查询
function search () {
  page.page = 1
  getTagAll()
}
// 清除
function clear () {
  query.labelName = ''
  query.minPerson = null
  query.maxPerson = null
  page.page = 1
  getTagAll()
}

let form = ref({});
const rules = {
  labelName: [
    { required: true, message: '请输入标签名称', trigger: 'blur' },
  ],
}
// 编辑
function edit (row) {
  if (row.labelId) {
    form.value.labelId = row.labelId
    labelEdit({ labelId: row.labelId }).then((res) => {
      if (res.code === '') {
        form.value.labelName = res.data.labelName
        form.value.remark = res.data.remark
      } else {
        ElMessage.error(res.message)
      }
    })
  }
  visible.data = true
}
function addTag () {
  form.value.labelId = null
  form.value.labelName = ''
  form.value.remark = ''
  visible.data = true
}
function saveTag () {
  tagFormRef.value.validate((valid) => {
    if (valid) {
      if (form.value.labelId) {
        labelUpdate(form.value).then((res) => {
          if (res.code === '') {
            ElMessage.success('修改成功')
            page.page = 1
            getTagAll()
          } else {
            ElMessage.error(res.message)
          }
        })
      } else {
        addLabel(form.value).then((res) => {
          if (res.code === '') {
            ElMessage.success('添加成功')
            page.page = 1
            getTagAll()
          } else {
            ElMessage.error(res.message)
          }
        })
      }
      visible.data = false
    } else {
      return false
    }
  })
}
function closeAddTag () {
  visible.data = false
}

let multipleSelection = ref([]);
function handleSelectionChange (val) {
  multipleSelection.value = val
}
function deleteTagFn (row) {
  var ids = []
  if (row?.labelId) {
    ids.push(row.labelId)
  } else {
    multipleSelection.value.forEach((item) => {
      ids.push(item.labelId)
    })
    if (ids.length === 0) {
      ElMessage.warning('请选择要删除的标签')
      return false
    }
  }
  deleteLabel({ ids }).then((res) => {
    if (res.code === '') {
      ElMessage.success('删除成功')
      page.page = 1
      getTagAll()
    } else {
      ElMessage.error(res.message)
    }
  })
}

onMounted(() => {
  getTagAll()
})
</script>

<style lang="scss" scoped>
.tagListPage {
  padding: 20px;
  margin-top: 20px;
  background-color: #FFFFFF;
  .formSearch {
    width: 100%;
    display: flex;
    flex-wrap: wrap;
    .searchItem {
      margin: 0 20px 20px 0;
      display: flex;
      align-items: center;
      span {
        min-width: 80px;
        text-align: center;
      }
      .label {
        max-width: 200px;
        min-width: 120px;
      }
      .nums {
        max-width: 120px;
        min-width: 80px;
      }
    }
  }
  // .fenye {
  //   margin-top: 20px;
  // }
}
</style>

<style scoped>
:deep(.btnList) .delCls {
  margin-left: 10px;
}
:deep(.group-dialog) .el-dialog__headerbtn .el-dialog__close {
  color: #ffffff;
}
:deep(.tagCustomer) .el-form-item__content {
  display: flex;
}
.tagCustomer {
  display: flex !important;
}
:deep(.tagCustomer) .el-form-item__content span {
  margin: 0 5px;
}
/* .tagListPage .formSearch /deep/ .el-form {
  display: flex;
}
.tagListPage .tagNameStyle /deep/ .el-form-item__content {
  width: 300px;
} */
:deep(.addTagBox) .el-form-item {
  display: flex;
}
:deep(.addTagBox) .el-form-item__content {
  width: 80%;
}
</style>
